package com.a.a.a;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCallback;
import android.bluetooth.BluetoothGattCharacteristic;
import android.bluetooth.BluetoothGattDescriptor;
import android.bluetooth.BluetoothGattService;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

public class j {
    class $1 extends Handler {

        public void handleMessage(Message arg3) {
            if (arg3.obj != null) {
                Toast.makeText(J, (String) arg3.obj, Toast.LENGTH_LONG).show();
            }
        }
    }

    class $4 extends Handler {
        public void handleMessage(Message arg1) {
            b(j.this);
        }
    }

    class $5 extends Handler {

        public void handleMessage(Message arg1) {
            c(j.this);
        }
    }

    class $6 implements Runnable {
        public void run() {
            if (e != null) {
                e.a(false);
            }
            d();
        }
    }

    class $7 implements BluetoothAdapter.LeScanCallback {

        @Override
        public void onLeScan(BluetoothDevice arg2, int arg3, byte[] arg4) {
            if (arg2 != null && (a(arg2.getName()))) {
                if (!e(j.this)) {
                    if (f(j.this) == 1) {
                    } else {
                        if (a(j.this, arg4)) {
                            d(j.this);
                            Message v3 = new Message();
                            v3.obj = arg2;
                            g(j.this).sendMessage(v3);
                        } else {
                        }

                        return;
                    }
                }

                return;
            }
        }
    }

    class $8 extends Handler {

        public void handleMessage(Message arg3) {
            if (arg3.obj != null && ((arg3.obj instanceof BluetoothDevice))) {
                BluetoothDevice device = (BluetoothDevice) arg3.obj;
                if (!e(j.this)) {
                    if (f(j.this) == 1) {
                    } else {
                        b(j.this, device.getAddress());
                        return;
                    }
                }

                return;
            }
        }
    }

    class $9 extends BluetoothGattCallback {
        public void onCharacteristicChanged(BluetoothGatt arg1, BluetoothGattCharacteristic arg2) {
            b(j.this, arg2.getValue());
        }

        public void onCharacteristicRead(BluetoothGatt arg1, BluetoothGattCharacteristic arg2,
                                         int arg3) {
            if (arg2.getUuid().equals(Common.d)) {
                try {
                    String v1_1 = new String(arg2.getValue(), "UTF-8");
                    if (e == null) {
                        return;
                    }
                    e.a(v1_1);
                } catch (UnsupportedEncodingException v1) {
                    v1.printStackTrace();
                }
            }
        }

        public void onConnectionStateChange(BluetoothGatt arg3, int arg4, int arg5) {
            int v3 = 2;
            if (arg5 == v3) {
                arg4 = 20;
                do {
                    if (h(j.this) != null) {
                        break;
                    }

                    long v0 = 20;
                    try {
                        Thread.sleep(v0);
                    } catch (InterruptedException v5) {
                        v5.printStackTrace();
                    }

                    --arg4;
                }
                while (arg4 > 0);

                if (h(j.this) == null) {
                    return;
                }

                a(j.this, v3);
                h(j.this).discoverServices();
                a(j.this, true);
                if (j.this.e == null) {
                    return;
                }

                j.this.e.a(true);
                return;
            }

            if (arg5 == 0) {
                a(j.this, 0);
                i(j.this);
                if (j.this.e == null) {
                    return;
                }

                j.this.e.a(false);
            }
        }

        public void onServicesDiscovered(BluetoothGatt arg3, int arg4) {
            if (arg4 == 0) {
                j(j.this).postDelayed(new Runnable() {
                    public void run() {
                        j.this.k(j.this);
                    }
                }, 100);
                j(j.this).postDelayed(new Runnable() {
                    public void run() {
                        n();
                    }
                }, 1000);
            }
        }
    }

    public l A;
    public l B;
    public l C;
    public l D;
    public l E;
    public l F;
    public Handler G;
    public int H;
    public BluetoothProfile I;
    private Activity J;
    private boolean K;
    private BluetoothAdapter L;
    private String M;
    private BluetoothGatt N;
    private Handler O;
    private int P;
    private boolean Q;
    private byte[] R;
    private Handler S;
    private boolean T;
    private Handler U;
    private Runnable V;
    private BluetoothAdapter.LeScanCallback W;
    private Handler X;
    private int Y;
    private int Z;
    public c a;
    private int aa;
    private int ab;
    private List<Integer> ac;
    private static final int ad = 20;
    private int ae;
    private final BluetoothGattCallback af;
    public f b;
    public d c;
    public b d;
    public i e;
    public g f;
    public e g;
    public h h;
    public l i;
    public l j;
    public l k;
    public l l;
    public l m;
    public l n;
    public l o;
    public l p;
    public l q;
    public l r;
    public l s;
    public l t;
    public l u;
    public l v;
    public l w;
    public m x;
    public m y;
    public m z;

    public j(Activity arg4) {
        super();
        this.J = null;
        this.K = false;
        this.O = new Handler();
        this.P = 0;
        this.Q = false;
        this.R = new byte[3];
        this.S = new Handler();
        this.T = false;
        this.G = new Handler();
        this.U = new Handler();
        this.V = new $6();
        this.W = new $7();
        this.X = new $8();
        this.Y = 0;
        this.Z = 0;
        this.aa = 0;
        this.ab = 0;
        this.ac = new ArrayList();
        this.H = -1;
        this.I = null;
        this.ae = 5;
        this.af = new $9();
        this.J = arg4;
        this.c();
        this.e();
    }

    static Activity a(j arg0) {
        return arg0.J;
    }

    private void a(int arg2, int arg3) {
        arg2 &= 255;
        arg3 &= 255;
        if (this.b != null) {
            this.b.a(arg2, arg3);
        }
    }

    private void a(int arg7, int arg8, int arg9, int arg10) {
        arg8 &= 255;
        arg7 &= 255;
        float v3 = 1f;
        if ((arg8 & 16) != 0) {
            if (!this.m.c) {
                this.m.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.m, v3, true);
                }
            }
        } else if (this.m.c) {
            this.m.a(0f, false);
            if (this.c != null) {
                this.c.a(this.m, 0f, false);
            }
        }

        if ((arg8 & 4) != 0) {
            if (!this.o.c) {
                this.o.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.o, v3, true);
                }
            }
        } else if (this.o.c) {
            this.o.a(0f, false);
            if (this.c != null) {
                this.c.a(this.o, 0f, false);
            }
        }

        if ((arg8 & 32) != 0) {
            if (!this.n.c) {
                this.n.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.n, v3, true);
                }
            }
        } else if (this.n.c) {
            this.n.a(0f, false);
            if (this.c != null) {
                this.c.a(this.n, 0f, false);
            }
        }

        if ((arg8 & 8) != 0) {
            if (!this.p.c) {
                this.p.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.p, v3, true);
                }
            }
        } else if (this.p.c) {
            this.p.a(0f, false);
            if (this.c != null) {
                this.c.a(this.p, 0f, false);
            }
        }

        if ((arg7 & 1) != 0) {
            if (!this.x.a.c) {
                this.x.a.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.x.a, v3, true);
                }
            }
        } else if (this.x.a.c) {
            this.x.a.a(0f, false);
            if (this.c != null) {
                this.c.a(this.x.a, 0f, false);
            }
        }

        if ((arg7 & 8) != 0) {
            if (!this.x.c.c) {
                this.x.c.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.x.c, v3, true);
                }
            }
        } else if (this.x.c.c) {
            this.x.c.a(0f, false);
            if (this.c != null) {
                this.c.a(this.x.c, 0f, false);
            }
        }

        if ((arg7 & 2) != 0) {
            if (!this.x.d.c) {
                this.x.d.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.x.d, v3, true);
                }
            }
        } else if (this.x.d.c) {
            this.x.d.a(0f, false);
            if (this.c != null) {
                this.c.a(this.x.d, 0f, false);
            }
        }

        if ((arg7 & 4) != 0) {
            if (!this.x.b.c) {
                this.x.b.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.x.b, v3, true);
                }
            }
        } else if (this.x.b.c) {
            this.x.b.a(0f, false);
            if (this.c != null) {
                this.c.a(this.x.b, 0f, false);
            }
        }

        if ((arg8 & 1) != 0) {
            if (!this.l.c) {
                this.l.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.l, v3, true);
                }
            }
        } else if (this.l.c) {
            this.l.a(0f, false);
            if (this.c != null) {
                this.c.a(this.l, 0f, false);
            }
        }

        if ((arg7 & 128) != 0) {
            if (!this.k.c) {
                this.k.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.k, v3, true);
                }
            }
        } else if (this.k.c) {
            this.k.a(0f, false);
            if (this.c != null) {
                this.c.a(this.k, 0f, false);
            }
        }

        if ((arg7 & 16) != 0) {
            if (!this.i.c) {
                this.i.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.i, v3, true);
                }
            }
        } else if (this.i.c) {
            this.i.a(0f, false);
            if (this.c != null) {
                this.c.a(this.i, 0f, false);
            }
        }

        if ((arg7 & 32) != 0) {
            if (!this.j.c) {
                this.j.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.j, v3, true);
                }
            }
        } else if (this.j.c) {
            this.j.a(0f, false);
            if (this.c != null) {
                this.c.a(this.j, 0f, false);
            }
        }

        if ((arg7 & 64) != 0) {
            if (!this.s.c) {
                this.s.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.s, v3, true);
                }
            }
        } else if (this.s.c) {
            this.s.a(0f, false);
            if (this.c != null) {
                this.c.a(this.s, 0f, false);
            }
        }

        if ((arg8 & 2) != 0) {
            if (!this.t.c) {
                this.t.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.t, v3, true);
                }
            }
        } else if (this.t.c) {
            this.t.a(0f, false);
            if (this.c != null) {
                this.c.a(this.t, 0f, false);
            }
        }

        if ((arg8 & 128) != 0) {
            if (!this.r.c) {
                this.r.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.r, v3, true);
                }
            }
        } else if (this.r.c) {
            this.r.a(0f, false);
            this.c.a(this.r, 0f, false);
        }

        if ((arg8 & 64) != 0) {
            if (!this.q.c) {
                this.q.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.q, v3, true);
                }
            }
        } else if (this.q.c) {
            this.q.a(0f, false);
            if (this.c != null) {
                this.c.a(this.q, 0f, false);
            }
        }

        arg7 = arg9 & 255;
        if ((arg7 & 16) != 0) {
            if (!this.u.c) {
                this.u.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.u, v3, true);
                }
            }
        } else if (this.u.c) {
            this.u.a(0f, false);
            if (this.c != null) {
                this.c.a(this.u, 0f, false);
            }
        }

        if ((arg7 & 8) != 0) {
            if (!this.v.c) {
                this.v.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.v, v3, true);
                }
            }
        } else if (this.v.c) {
            this.v.a(0f, false);
            if (this.c != null) {
                this.c.a(this.v, 0f, false);
            }
        }

        if ((arg7 & 1) != 0) {
            if (!this.w.c) {
                this.w.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.w, v3, true);
                }
            }
        } else if (this.w.c) {
            this.w.a(0f, false);
            if (this.c != null) {
                this.c.a(this.w, 0f, false);
            }
        }

        arg7 = arg10 & 255;
        if ((arg7 & 1) != 0) {
            if (!this.A.c) {
                this.A.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.A, v3, true);
                }
            }
        } else if (this.A.c) {
            this.A.a(0f, false);
            if (this.c != null) {
                this.c.a(this.A, 0f, false);
            }
        }

        if ((arg7 & 2) != 0) {
            if (!this.B.c) {
                this.B.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.B, v3, true);
                }
            }
        } else if (this.B.c) {
            this.B.a(0f, false);
            if (this.c != null) {
                this.c.a(this.B, 0f, false);
            }
        }

        if ((arg7 & 4) != 0) {
            if (!this.C.c) {
                this.C.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.C, v3, true);
                }
            }
        } else if (this.C.c) {
            this.C.a(0f, false);
            if (this.c != null) {
                this.c.a(this.C, 0f, false);
            }
        }

        if ((arg7 & 8) != 0) {
            if (!this.D.c) {
                this.D.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.D, v3, true);
                }
            }
        } else if (this.D.c) {
            this.D.a(0f, false);
            if (this.c != null) {
                this.c.a(this.D, 0f, false);
            }
        }

        if ((arg7 & 16) != 0) {
            if (!this.E.c) {
                this.E.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.E, v3, true);
                }
            }
        } else if (this.E.c) {
            this.E.a(0f, false);
            if (this.c != null) {
                this.c.a(this.E, 0f, false);
            }
        }

        if ((arg7 & 32) != 0) {
            if (!this.F.c) {
                this.F.a(v3, true);
                if (this.c != null) {
                    this.c.a(this.F, v3, true);
                }
            }
        } else if (this.F.c) {
            this.F.a(0f, false);
            if (this.c != null) {
                this.c.a(this.F, 0f, false);
            }
        }
    }

    static void a(j arg0, int arg1) {
        arg0.P = arg1;
    }

    static void a(j arg0, boolean arg1) {
        arg0.K = arg1;
    }

    private boolean a(int paramInt) {
        switch (paramInt) {
            default:
                return true;
            case 115:
                return false;
            case 114:
                return false;
            case 113:
                return false;
            case 112:
                return false;
            case 111:
                return false;
            case 110:
                return false;
            case 109:
                return false;
            case 108:
                return false;
            case 106:
                return false;
            case 105:
                return false;
            case 104:
                return false;
            case 103:
                return false;
            case 102:
                return false;
            case 101:
                return false;
            case 100:
                break;
        }
        return false;
    }

    static boolean a(j arg0, String arg1) {
        return arg0.c(arg1);
    }

    static boolean a(j arg0, byte[] arg1) {
        return arg0.a(arg1);
    }

    private boolean a(byte[] paramArrayOfbyte) {
        if (paramArrayOfbyte != null)
            for (byte b1 = 0; b1 < paramArrayOfbyte.length; b1++) {
                if (paramArrayOfbyte[b1] == 1) {
                    b1 = paramArrayOfbyte[b1 + 1];
                    return ((b1 & 0x1) > 0 || (b1 & 0x2) > 0);
                }
            }
        return false;
    }

    private boolean a(String paramString) {
        return !(!paramString.toLowerCase().contains("q1") && !paramString.toLowerCase().contains("d1"));
    }


    public void a() {
        if (Build.VERSION.SDK_INT < 18) {
            this.b("系统不支持");
            if (this.e != null) {
                this.e.a(false);
            }
        } else if (this.L != null) {
            if (!this.L.isEnabled()) {
                if (this.e != null) {
                    this.e.a(false);
                }
            } else if (!this.K && !this.Q) {
                if (this.j()) {
                    this.l();
                } else {
                    this.h();
                }
            }
        } else if (this.e != null) {
            this.e.a(false);
        }
    }

    public boolean a(BluetoothDevice arg5, String arg6) {
        if (this.L != null) {
            if (arg6 == null) {
            } else {
                if (this.M != null && (arg6.equals(this.M)) && this.N != null) {
                    if (this.N.connect()) {
                        this.P = 1;
                        return true;
                    } else {
                        this.b("手柄连接失败");
                        return false;
                    }
                }

                if (arg5 == null) {
                    this.b("手柄连接失败");
                    return false;
                }

                if (this.a(arg5.getName())) {
                    this.N = arg5.connectGatt(this.J, false, this.af);
                    this.M = arg6;
                    this.P = 1;
                }

                return true;
            }
        }

        return false;
    }

    private int b(int arg3) {
        int v1 = 127;
        if (Math.abs(arg3) > v1) {
            if (arg3 > 0) {
                return v1;
            }

            return -127;
        }

        return arg3;
    }

    private void b(int arg7, int arg8, int arg9, int arg10) {
        if (this.a != null) {
            this.a.a(this.y, arg7 & 255, arg8 & 255);
            this.a.a(this.z, arg9 & 255, arg10 & 255);
        }

        arg7 = (arg7 & 255) - 128;
        arg8 = (arg8 & 255) - 128;
        arg9 = (arg9 & 255) - 128;
        arg10 = (arg10 & 255) - 128;
        int v0 = ((int) Math.sqrt(((double) (arg7 * arg7 + arg8 * arg8))));
        int v1 = 0;
        float v2 = 1.4f;
        int v3 = 20;
        if (v0 > v3) {
            int v4 = v0 - 20;
            arg7 = this.b(((int) ((((float) (arg7 * v4 / v0))) * v2)));
            arg8 = this.b(((int) ((((float) (arg8 * v4 / v0))) * v2)));
        } else {
            arg7 = 0;
            arg8 = 0;
        }

        v0 = ((int) Math.sqrt(((double) (arg9 * arg9 + arg10 * arg10))));
        if (v0 > v3) {
            v1 = v0 - 20;
            arg10 = ((int) ((((float) (arg10 * v1 / v0))) * v2));
            v1 = this.b(((int) ((((float) (arg9 * v1 / v0))) * v2)));
            arg9 = this.b(arg10);
        } else {
            arg9 = 0;
        }

        float v10 = 128f;
        float v7 = (((float) arg7)) / v10;
        float v8 = (((float) arg8)) / v10;
        this.y.a(v7, v8);
        if (this.d != null) {
            this.d.a(this.y, v7, v8);
        }

        v7 = (((float) v1)) / v10;
        v8 = (((float) arg9)) / v10;
        this.z.a(v7, v8);
        if (this.d != null) {
            this.d.a(this.z, v7, v8);
        }
    }

    static void b(j arg0) {
        arg0.t();
    }

    static void b(j arg0, byte[] arg1) {
        arg0.b(arg1);
    }

    private void b(byte[] arg6) {
        if (arg6.length <= 3) {
            return;
        }

        int v2 = 9;
        if (arg6.length == 20 && (arg6[0] & 0xFF) == 172 && (arg6[1] & 0xFF) == 192) {
            if ((arg6[9] & 0x40) != 0 || (arg6[9] & 0x4) != 0) {
                this.ab = 1;
            } else {
                this.ab = 0;
            }
        }

        if (arg6.length == 14) {
            if ((arg6[9] & 0x80) == 128) {
                this.aa = 1;
                d(arg6);
            } else {
                this.aa = 0;
                c(arg6);
            }
        }

        if (this.ab == this.aa) {
            this.Z = this.aa;
        } else if (this.aa > 0) {
            this.Z = this.aa;
        } else if (this.ab > 0) {
            this.Z = this.ab;
        }
        if (this.Y != this.Z) {
            this.Y = this.Z;
            if (this.f != null)
                this.f.a(this.Z);
        }
    }

    private void b(String arg2) {
        Message v0 = new Message();
        v0.obj = arg2;
        v0.what = 0;
        this.S.sendMessage(v0);
    }

    static boolean b(j arg0, String arg1) {
        return arg0.d(arg1);
    }

    public void b() {
        this.g();
    }

    public void c() {
        this.q = new l(com.a.a.a.n.a.BTN_THUMBL);
        this.r = new l(com.a.a.a.n.a.BTN_THUMBR);
        this.s = new l(com.a.a.a.n.a.BTN_SELECT);
        this.t = new l(com.a.a.a.n.a.BTN_START);
        this.i = new l(com.a.a.a.n.a.BTN_A);
        this.j = new l(com.a.a.a.n.a.BTN_B);
        this.k = new l(com.a.a.a.n.a.BTN_X);
        this.l = new l(com.a.a.a.n.a.BTN_Y);
        this.m = new l(com.a.a.a.n.a.BTN_LT);
        this.n = new l(com.a.a.a.n.a.BTN_RT);
        this.o = new l(com.a.a.a.n.a.BTN_LB);
        this.p = new l(com.a.a.a.n.a.BTN_RB);
        this.u = new l(com.a.a.a.n.a.BTN_BACK);
        this.w = new l(com.a.a.a.n.a.BTN_MENU);
        this.v = new l(com.a.a.a.n.a.BTN_HOME);
        this.A = new l(com.a.a.a.n.a.BTN_C);
        this.B = new l(com.a.a.a.n.a.BTN_Z);
        this.C = new l(com.a.a.a.n.a.BTN_M1);
        this.D = new l(com.a.a.a.n.a.BTN_M2);
        this.E = new l(com.a.a.a.n.a.BTN_M3);
        this.F = new l(com.a.a.a.n.a.BTN_M4);
        this.x = new m(com.a.a.a.n.a.DPAD_CROSS_KEY);
        this.y = new m(com.a.a.a.n.a.DPAD_THUMBSTICK_L);
        this.z = new m(com.a.a.a.n.a.DPAD_THUMBSTICK_R);
    }

    private boolean c(String paramString) {
        return (paramString != null && (paramString.toLowerCase().startsWith("feizhi") || paramString.toLowerCase().startsWith("flydigi") || paramString.toLowerCase().endsWith("fdm")));
    }

    private void c(byte[] arg5) {
        this.a(arg5[4], arg5[5], arg5[8], arg5[9]);
        this.b(arg5[0], arg5[1], arg5[2], arg5[3]);
        this.a(arg5[6], arg5[7]);
        if (this.h != null) {
            this.h.a(arg5[11], arg5[12], arg5[13]);
        }
    }

    static void c(j arg0) {
        arg0.r();
    }

    private void d(byte[] arg15) {
        List<Integer> v0 = new ArrayList();
        int v2 = arg15[1] & 255;
        int v3 = 2;
        int v4 = arg15[v3] & 255;
        int v5 = arg15[3] & 255;
        int v6 = 4;
        int v7 = arg15[v6] & 255;
        int v8 = arg15[5] & 255;
        int v9 = 8;
        int v10 = 9;
        int v11 = arg15[11] & 255;
        int v12 = arg15[12] & 255;
        int v13 = arg15[13] & 255;
        if (v8 != 0) {
            ((List) v0).add(Integer.valueOf(v8));
        }

        if (v7 != 0) {
            ((List) v0).add(Integer.valueOf(v7));
        }

        if (v5 != 0) {
            ((List) v0).add(Integer.valueOf(v5));
        }

        if (v4 != 0) {
            ((List) v0).add(Integer.valueOf(v4));
        }

        if (v2 != 0) {
            ((List) v0).add(Integer.valueOf(v2));
        }

        if ((arg15[v9] & 1) != 0) {
            ((List) v0).add(Integer.valueOf(109));
        }

        if ((arg15[v9] & v3) != 0) {
            ((List) v0).add(Integer.valueOf(108));
        }

        if ((arg15[v9] & v6) != 0) {
            ((List) v0).add(Integer.valueOf(111));
        }

        if ((arg15[v9] & v9) != 0) {
            ((List) v0).add(Integer.valueOf(110));
        }

        if ((arg15[v9] & 16) != 0) {
            ((List) v0).add(Integer.valueOf(113));
        }

        if ((arg15[v9] & 32) != 0) {
            ((List) v0).add(Integer.valueOf(112));
        }

        if ((arg15[v9] & 64) != 0) {
            ((List) v0).add(Integer.valueOf(115));
        }

        if ((arg15[v9] & 128) != 0) {
            ((List) v0).add(Integer.valueOf(114));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & 1) != 0) {
            ((List) v0).add(Integer.valueOf(100));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & v6) != 0) {
            ((List) v0).add(Integer.valueOf(102));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & v3) != 0) {
            ((List) v0).add(Integer.valueOf(101));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & 16) != 0) {
            ((List) v0).add(Integer.valueOf(103));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & v9) != 0) {
            ((List) v0).add(Integer.valueOf(104));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & 32) != 0) {
            ((List) v0).add(Integer.valueOf(105));
        }

        if ((arg15[v10] & 128) != 0 && (arg15[v10] & 64) != 0) {
            ((List) v0).add(Integer.valueOf(106));
        }

        ArrayList v15 = new ArrayList();
        v2 = 0;
        for (v3 = 0; v3 < this.ac.size(); ++v3) {
            if (!((List) v0).contains(this.ac.get(v3))) {
                o v4_1 = new o();
                v4_1.a(this.ac.get(v3).intValue());
                v4_1.a(false);
                v4_1.b(this.a(this.ac.get(v3).intValue()));
                v15.add(v4_1);
            }
        }

        while (v2 < ((List) v0).size()) {
            if (!this.ac.contains(((List) v0).get(v2))) {
                o v3_1 = new o();
                v3_1.a(v0.get(v2));
                v3_1.a(true);
                v3_1.b(this.a(v0.get(v2).intValue()));
                v15.add(v3_1);
            }

            ++v2;
        }

        if (this.g != null) {
            this.g.a(v15);
        }

        this.ac.clear();
        this.ac.addAll(((Collection) v0));
        if (this.h != null) {
            this.h.a(v11, v12, v13);
        }
    }

    private boolean d(String arg6) {
        if (this.L != null) {
            if (arg6 == null) {
            } else {
                if (this.M != null && (arg6.equals(this.M)) && this.N != null) {
                    if (this.N.connect()) {
                        this.P = 1;
                        return true;
                    } else {
                        this.b("手柄连接失败");
                        return false;
                    }
                }

                BluetoothDevice v0 = this.L.getRemoteDevice(arg6);
                if (v0 == null) {
                    this.b("手柄连接失败");
                    return false;
                }

                if (this.a(v0.getName())) {
                    this.N = v0.connectGatt(this.J, false, this.af);
                    this.M = arg6;
                    this.P = 1;
                }

                return true;
            }
        }

        return false;
    }

    static void d(j arg0) {
        arg0.s();
    }

    public void d() {
        this.f();
    }

    public void e() {
        Object v0 = this.J.getSystemService(Context.BLUETOOTH_SERVICE);
        if (v0 == null) {
            return;
        }

        this.L = ((BluetoothManager) v0).getAdapter();
        if (this.L == null) {
            return;
        }
    }

    private void e(byte[] arg3) {
        if (this.N != null) {
            if (!this.Q) {
            } else {
                BluetoothGattService v0 = this.N.getService(Common.l);
                if (v0 == null) {
                    this.b("Rx service not found!");
                    this.m();
                    return;
                } else {
                    BluetoothGattCharacteristic v0_1 = v0.getCharacteristic(Common.m);
                    if (v0_1 == null) {
                        this.b("Rx charateristic not found!");
                        this.m();
                        return;
                    } else {
                        v0_1.setValue(arg3);
                        this.N.writeCharacteristic(v0_1);
                        return;
                    }
                }
            }
        }
    }

    static boolean e(j arg0) {
        return arg0.K;
    }

    static int f(j arg0) {
        return arg0.P;
    }

    public void f() {
        this.m();
        this.T = true;
    }

    static Handler g(j arg0) {
        return arg0.X;
    }

    public void g() {
        this.O.post(new Runnable() {
            public void run() {
                m();
            }
        });
    }

    static BluetoothGatt h(j arg0) {
        return arg0.N;
    }

    public void h() {
        if (this.L != null) {
            new Thread(new Runnable() {
                public void run() {
                    if (!e(j.this) && f(j.this) == 0) {
                        c(j.this);
                    }
                }
            }).start();
        }
    }

    static void i(j arg0) {
        arg0.u();
    }

    public void i() {
        this.s();
    }

    static Handler j(j arg0) {
        return arg0.O;
    }

    public boolean j() {

        Set<BluetoothDevice> bondedDevices = this.L.getBondedDevices();
        if (bondedDevices != null && bondedDevices.size() > 0) {

            for (BluetoothDevice bluetoothDevice : bondedDevices) {
                if (bluetoothDevice != null && c(bluetoothDevice.getName()))
                    return true;
            }

        }
        return false;
    }

    static void k(j arg0) {
        arg0.v();
    }

    public void k() {
        if (this.H >= 0 && this.I != null) {
            this.L.closeProfileProxy(this.H, this.I);
            this.H = -1;
            this.I = null;
        }
    }

    static boolean l(j arg0) {
        return arg0.T;
    }

    public void l() {

        this.L.getProfileProxy(this.J, new BluetoothProfile.ServiceListener() {
            @Override
            public void onServiceConnected(int profile, BluetoothProfile proxy) {
                H = profile;
                I = proxy;

                if (proxy.getConnectedDevices().size() > 0) {
                    for (BluetoothDevice connectedDevice : proxy.getConnectedDevices()) {
                        String str = connectedDevice.getName();
                        if (a(str)) {
                            a(connectedDevice, connectedDevice.getAddress());
                            k();
                            break;
                        }
                    }
                } else {
                    k();
                    h();
                }
            }

            @Override
            public void onServiceDisconnected(int profile) {

            }
        }, 4);
    }

    public void m() {
        if (this.N != null) {
            this.N.disconnect();
        }

        this.K = false;
        this.Q = false;
        this.P = 0;
    }

    static int m(j arg0) {
        return arg0.ae;
    }

    public void n() {
        if (this.N == null) {
            return;
        }

        BluetoothGattService v0 = this.N.getService(Common.e);
        if (v0 == null) {
            return;
        }

        BluetoothGattCharacteristic v0_1 = v0.getCharacteristic(Common.d);
        if (v0_1 == null) {
            return;
        }

        this.N.readCharacteristic(v0_1);
    }

    public String o() {
        return Common.f;
    }

    private void p() {
        new Thread(new Runnable() {
            public void run() {
                while (!l(j.this)) {
                    if (m(j.this) <= 0) {
                    } else {
                        long v0 = 1000;
                        try {
                            Thread.sleep(v0);
                        } catch (InterruptedException v0_1) {
                            v0_1.printStackTrace();
                        }
                        G.sendEmptyMessage(0);
                        continue;
                    }

                    return;
                }
            }
        }).start();
    }

    private boolean q() {
        try {
            if (this.L != null) {
                if (this.L.isEnabled()) {
                    return true;
                }

                return false;
            }
        } catch (Exception v1) {
            v1.printStackTrace();
        }

        return false;
    }

    private void r() {
        if (!this.L.isDiscovering()) {
            this.L.startLeScan(this.W);
            this.U.postDelayed(this.V, 8000);
        }
    }

    private void s() {
        if (!this.q()) {
            return;
        }

        this.L.stopLeScan(this.W);
        this.U.removeCallbacks(this.V);
    }

    private void t() {
        if ((this.K) && (this.Q) && this.ae > 0) {
            --this.ae;
            this.R[0] = -84;
            this.R[1] = -17;
            this.R[2] = 0;
            this.e(this.R);
        }
    }

    private void u() {
        if (this.N == null) {
            return;
        }

        this.M = null;
        this.N.close();
        this.N = null;
        this.P = 0;
        this.K = false;
        this.Q = false;
    }

    private void v() {
        if (this.N == null) {
            return;
        }

        BluetoothGattService v0 = this.N.getService(Common.l);
        if (v0 == null) {
            this.b("手柄连接出错");
            this.m();
            return;
        }

        BluetoothGattCharacteristic v0_1 = v0.getCharacteristic(Common.n);
        if (v0_1 == null) {
            this.b("手柄连接出错");
            this.m();
            return;
        }

        this.N.setCharacteristicNotification(v0_1, true);
        BluetoothGattDescriptor v0_2 = v0_1.getDescriptor(Common.k);
        v0_2.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
        this.N.writeDescriptor(v0_2);
        this.Q = true;
        this.ae = 5;
        this.p();
    }
}

